/*
 * 
 */
package com.flying.dcss.userpermissions.action;

import java.util.LinkedList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.flying.dcss.app.SessionName;
import com.flying.dcss.dcmanager.bo.impl.DcssDCInfo;
import com.flying.dcss.dcmanager.bo.impl.DcssDCTreeInfo;
import com.flying.dcss.dcmanager.service.impl.DCInfoService;
import com.flying.dcss.dcmanager.service.impl.DCTreeService;
import com.flying.dcss.userpermissions.bo.impl.DcssUser;
import com.flying.dcss.userpermissions.bo.impl.DcssUserInfo;
import com.flying.dcss.userpermissions.service.impl.UserInfoService;
import com.flying.dcss.userpermissions.service.impl.UserService;
import com.flying.dcss.viewmodel.impl.DCListViewModel;
import com.opensymphony.xwork2.ActionSupport;

// TODO: Auto-generated Javadoc
/**
 * The Class GetSetAgentAction.
 */
public class GetSetAgentAction extends ActionSupport {

	/** The user service. */
	private UserService userService;
	
	/** The user info service. */
	private UserInfoService userInfoService;
	
	/** The dc info service. */
	private DCInfoService dcInfoService;
	
	/** The dc tree service. */
	private DCTreeService dcTreeService;

	/**
	 * Sets the user service.
	 *
	 * @param userService the new user service
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * Sets the user info service.
	 *
	 * @param userInfoService the new user info service
	 */
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	/**
	 * Sets the dc info service.
	 *
	 * @param dcInfoService the new dc info service
	 */
	public void setDcInfoService(DCInfoService dcInfoService) {
		this.dcInfoService = dcInfoService;
	}

	/**
	 * Sets the dc tree service.
	 *
	 * @param dcTreeService the new dc tree service
	 */
	public void setDcTreeService(DCTreeService dcTreeService) {
		this.dcTreeService = dcTreeService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		DcssUser user = (DcssUser) ServletActionContext.getRequest()
				.getSession().getAttribute(SessionName.UserSession);

		List<DCListViewModel> models = new LinkedList<DCListViewModel>();

		List<DcssDCInfo> dcInfos = dcInfoService.getByManagerId(user.getId());

		// Complete DCListViewModel
		for (DcssDCInfo item : dcInfos) {
			// Get UserInfo.
			DcssUserInfo userInfo = userInfoService.getUserInfoByUserId(item
					.getManagerId());

			// Get floor.
			DcssDCTreeInfo floor = dcTreeService.getById(item.getFatherId());
			// Get building.
			DcssDCTreeInfo building = dcTreeService
					.getById(floor.getFatherId());
			// Get city.
			DcssDCTreeInfo city = dcTreeService.getById(building.getFatherId());

			DCListViewModel model = new DCListViewModel(item, city
					.getDescription(), building.getDescription(), floor
					.getDescription(), userInfo.getRealName(), userInfo
					.getEmpoyeeNum());

			models.add(model);
		}

		ServletActionContext.getRequest().setAttribute(
				SessionName.DcListSession, models);

		return this.SUCCESS;
	}
}
